﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SilveR.DataValidators;
using SilveR.Views.StatsViews;
using System.Windows;
using BindableDataGrid.Data;

namespace SilveR.DataValidators
{
    class SummaryStatsValidator : DataValidator
    {
        private readonly SummaryStatsVariables ssVariables;

        public SummaryStatsValidator(SummaryStatsVariables ss)
            : base(ss.SelectedDataHandler)
        {
            ssVariables = ss;
        }

        public override bool Validate()
        {
            //first just check to ensure that the user has actually selected something to output!
            if (!ssVariables.MeanSelected && !ssVariables.NSelected && !ssVariables.StDevSelected && !ssVariables.VariancesSelected && !ssVariables.StErrSelected && !ssVariables.MinAndMaxSelected && !ssVariables.MedianQuartileSelected && !ssVariables.CoefficientVariationSelected)
            {
                MessageBox.Show("You have not selected anything to output!", "Validate Data", MessageBoxButton.OK);
                return false;
            }

            //Create a list of all variables
            List<string> allVars = new List<string>();
            allVars.AddRange(ssVariables.ResponseColumnNames);
            allVars.Add(ssVariables.FirstCatFactor);
            allVars.Add(ssVariables.SecondCatFactor);
            allVars.Add(ssVariables.ThirdCatFactor);
            allVars.Add(ssVariables.FourthCatFactor);
            if (!CheckColumnNames(allVars)) return false;

            //Create a list of categorical variables selected (i.e. the cat factors)
            List<string> categorical = new List<string>();
            if (!String.IsNullOrEmpty(ssVariables.FirstCatFactor)) categorical.Add(ssVariables.FirstCatFactor);
            if (!String.IsNullOrEmpty(ssVariables.SecondCatFactor)) categorical.Add(ssVariables.SecondCatFactor);
            if (!String.IsNullOrEmpty(ssVariables.ThirdCatFactor)) categorical.Add(ssVariables.ThirdCatFactor);
            if (!String.IsNullOrEmpty(ssVariables.FourthCatFactor)) categorical.Add(ssVariables.FourthCatFactor);

            //Go throught each response
            foreach (string response in ssVariables.ResponseColumnNames)
            {
                if (!CheckIsNumeric(response))
                {
                    MessageBox.Show("Error: The response variable (" + response + ") selected contains non-numerical data. Please amend the dataset prior to running the analysis.", "Validate Data", MessageBoxButton.OK);
                    return false;
                }

                foreach (DataRow row in DataTable.Rows)
                {
                    CheckTransformations(row, ssVariables.Transformation, response, "response");
                }

                foreach (string catFactor in categorical) //go through each categorical factor and do the check on each
                {
                    //Check that each level has replication
                    Dictionary<string, int> levelResponses = ResponsesPerLevel(catFactor, response);
                    foreach (KeyValuePair<string, int> level in levelResponses)
                    {
                        if (level.Value == 0)
                        {
                            MessageBox.Show("Error: There are no observations recorded on the levels of one of the factors. Please amend the dataset prior to running the analysis.", "Validate Data", MessageBoxButton.OK);
                            return false;
                        }
                        else if (level.Value < 2)
                        {
                            MessageBox.Show("Error: There is no replication in one or more of the levels of the categorical factor (" + catFactor + ").  Please amend the dataset prior to running the analysis.", "Validate Data", MessageBoxButton.OK);
                            return false;
                        }
                    }

                    //check response and cat factors contain values
                    if (!CheckResponseAndTreatmentsNotBlank(response, catFactor, "categorisation factor")) return false;
                }
            }

            //if get here then no errors so return true
            return true;
        }
    }
}